package com.autotemplate.util.vo;


import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import java.io.PrintWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

/**
 * function
 * Created by lhlong on 2016/12/2.
 */
public class VelocityLongUtil {
    private String velocityPath ;
    private String velocityDestination;
    private String vmName;

    public VelocityLongUtil(String velocityPath, String velocityDestination,String vmName){
        this.velocityPath = velocityPath;
        this.velocityDestination = velocityDestination;
        this.vmName = vmName;
    }

    public void generateByVelocity(){
        VelocityEngine ve = new VelocityEngine();

        ve.setProperty(VelocityEngine.RUNTIME_LOG,"longLog");
        ve.setProperty("file.resource.loader.path", velocityPath);
        ve.init();

        VelocityContext vcontext = new VelocityContext();
        vcontext.put("hello","Helloworld!");
        Template template = null;

        try{
            template = ve.getTemplate(vmName);

            Writer pw = new PrintWriter(velocityDestination);
            template.merge(vcontext,pw);
            pw.flush();

//            System.out.println("template:"+pw);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     *按照values生成对应文件
     * @param values
     */
    public void generateByValue(Map<String,String> values,String tableName){
        VelocityEngine ve = new VelocityEngine();

        ve.setProperty(VelocityEngine.RUNTIME_LOG,"longLog");
        ve.setProperty("file.resource.loader.path", velocityPath);
        ve.init();

        VelocityContext vcontext = new VelocityContext();
//        Iterator valueMapIterator = values.entrySet().iterator();
//        while (valueMapIterator.hasNext()) {
//            Map.Entry element = (Map.Entry) valueMapIterator.next();
//            String key = (String) element.getKey();
//            String value = (String) element.getValue();
//            vcontext.put(key,value);
//        }
        vcontext.put("columns",values);
        vcontext.put("tableName",tableName);
        Template template = null;

        try{
            template = ve.getTemplate(vmName);
            Writer pw = new PrintWriter(velocityDestination);
            template.merge(vcontext,pw);
            pw.flush();
        }
        catch(Exception e){
            e.printStackTrace();
        }

    }

    public String getVelocityPath() {
        return velocityPath;
    }

    public void setVelocityPath(String velocityPath) {
        this.velocityPath = velocityPath;
    }

    public String getVelocityDestination() {
        return velocityDestination;
    }

    public void setVelocityDestination(String velocityDestination) {
        this.velocityDestination = velocityDestination;
    }

    public String getVmName() {
        return vmName;
    }

    public void setVmName(String vmName) {
        this.vmName = vmName;
    }
}
